package com.gdk.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdk.service.PromotionService;
import com.gdk.service.SmallticketService;
import com.gdk.utils.XVDateUtils;


@Controller
public class SmallticketController {
	@Autowired
	private SmallticketService smallticketService;
	@Autowired
	PromotionService promotionService;
	
	@RequestMapping("/toAddSmallticket.do")
	public String addSmallticket() {
		return "jsp/zhekou/zhekou-add";
	}
	
	@RequestMapping("/addSmallticket.do")
	public String addSmallticket(@RequestParam Map<String, Object> map) {
		System.out.println("传递过来的参数:"+map);
		if (!map.containsKey("editorValue")) {
			map.put("editorValue", "");
		}
		int row = this.addSmallticket1(map);
		return "redirect:/findAllSmallticket.do?page=1";
	}
	
	public int addSmallticket1(Map<String, Object> map) {
		int row  = smallticketService.addSmallticket(map);
		while(row<=0) {
			row = this.addSmallticket1(map);
		}
		return row;
	}
	
	
	@RequestMapping("/findAllSmallticket.do")
	public String findAllActicle(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request,ModelMap map1) {
		String str = "";
		//在这里进行全部抵扣券的查询
		//进行分页
				int page;//记录页码
				if (map.get("page") != null) {
					page = Integer.parseInt(map.get("page").toString());
				} else {
					page = 1;
				}

				
				map.put("startNum", (page - 1) * 15);
				System.out.println("查询之前参数值为："+map);
				List<Map<String, Object>> list = smallticketService.allSmallticket(map);
				//在这里进行list的解析
				/*for (Map<String, Object> map2 : list) {
					String[] nid = map2.get("n_id").toString().split(",");
					for (int i = 0; i < nid.length; i++) {
						List<Map<String, Object>> liMaps = articleService.selectNavigation1(nid[i]);
						//下面进行字符串的拼接
						str+=",";
						str+=liMaps.get(0).get("n_name");	
					}
					map2.put("n_name", str.substring(1,str.length()));
					str = "";
					
				}*/
				int count = smallticketService.countSmallticket();//查询活动的数量
				int pageNum = 0;// 总页数
				if (count % 15 == 0) {
					pageNum = count / 15;
				} else {
					pageNum = (int) count / 15 + 1;
				}
				Vector<Integer> pageArr = new Vector<Integer>();// 定义一个可变数组用来储存动态的页码数据
				int start = 1;// 定义一个起始页
				if (page >= 10) {
					start = page / 10;
					start = start * 10;
				}
				int num = start;
				while (!(num > pageNum || num > start + 10)) {
					pageArr.add(new Integer(num));
					++num;
				}
				
				
				
				//在这里进行用户商品分页得查询 
//				List<Map<String, Object>> list = goodsService.selectMyShopGoods(maps.get(0));
				if (list.size()>0) {
					map1.addAttribute("smallticketList", list);
					request.setAttribute("pageList", pageArr);
					request.setAttribute("pageNum", pageNum);
					request.setAttribute("page", page);
					request.setAttribute("count", count);
					map1.addAttribute("msg", "有文章");
				}else {
					map1.addAttribute("smallticketList", list);
					request.setAttribute("pageList", pageArr);
					request.setAttribute("pageNum", pageNum);
					request.setAttribute("page", page);
					request.setAttribute("count", count);
					map1.addAttribute("msg", "您还没有上传文章哦！");			
				}
		return "jsp/zhekou/smallticket-list";
	}
	
	
	@RequestMapping("/updateThisSmallticketbefore.do")
	public String updateThisSmallticketbefore(@RequestParam Map<String, Object> map,HttpServletRequest request,HttpSession session) {			
		List<Map<String, Object>> list = smallticketService.SmallticketDetail(map);
		/*//查询活动的排序
		List<Integer> list2 = activityService.sortActivity();
		Collections.sort(list2);
		session.setAttribute("acSort", list2);
		int size = list2.size();
		System.out.println("daxiao    :"+size);
		//判断当前的文章在那个排序字段上
		int sort = list2.indexOf(list.get(0).get("displayorder"))+1;
		list.get(0).put("starttime", list.get(0).get("starttime").toString().substring(0, list.get(0).get("starttime").toString().lastIndexOf(".")));
		list.get(0).put("endtime", list.get(0).get("endtime").toString().substring(0, list.get(0).get("endtime").toString().lastIndexOf(".")));
		list.get(0).put("joinstime", list.get(0).get("joinstime").toString().substring(0, list.get(0).get("joinstime").toString().lastIndexOf(".")));
		list.get(0).put("joinetime", list.get(0).get("joinetime").toString().substring(0, list.get(0).get("joinetime").toString().lastIndexOf(".")));
		list.get(0).put("sort", sort);
		list.get(0).put("size", size);*/
		request.setAttribute("smallticketDetail", list.get(0));
		return "jsp/zhekou/smallticket-update";
	}
	
	@RequestMapping("/updateSmallticket.do")
	public String updateSmallticket(@RequestParam Map<String, Object> map) {
		System.out.println("修改的参位:"+map);
		//进行修改操作
		int row = this.updateSmallticket1(map);
		return "redirect:/findAllSmallticket.do?page=1";
	}
	
	public int updateSmallticket1(Map<String, Object> map) {
		int row  = smallticketService.updateSmallticket(map);
		while(row<=0) {
			row = this.updateSmallticket1(map);
		}
		return row;
	}
	
	@ResponseBody
	@RequestMapping("/deleteSmallticket.do")
	public int deleteSmallticket(@RequestParam Map<String, Object> map) {
		int row = 0;
		//在这里进行数据的查找工作
		List<Map<String, Object>> list = smallticketService.selectbindticket(map);
		//证明绑定到正在进行的活动中
		if (list.size()>0) {
			row = 0;
		}else {
			row =this.deleteSmallticket1(map);
		}
		
		
		return row;
	}
	
	
	public int deleteSmallticket1(Map<String, Object> map) {
		int row = smallticketService.deleteSmallticket(map);
		while (row<=0) {
		 row = this.deleteSmallticket1(map);
		}
		return row;
	}



	
	
	
	
	@RequestMapping("/preAddCard.do")
	public String preAddCard(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		List<Map<String, Object>> allPromotionByFlagAndStatus = promotionService.findAllPromotionByFlagAndStatus();
		request.setAttribute("allPromotionByFlagAndStatus", allPromotionByFlagAndStatus);
		return "jsp/zhekou/zhekou_add";		
	}
	/**
	 * 新增抵扣卡
	 * @author xuwei
	 * time 2019/02/19 15:43
	 * */
	@ResponseBody
	@RequestMapping("/addCard.do")
	public int addCard(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		
		map.put("c_creattime", XVDateUtils.getCurrenTimeMillis());
		map.put("c_btime", XVDateUtils.stringDateToMillis(map.get("c_btime").toString()));
		map.put("c_etime", XVDateUtils.stringDateToMillis(map.get("c_etime").toString()));
		System.out.println("参数："+map);
		//System.out.println(CommonUtils.StripHT(map.get("t_info").toString()));
		//下面进行文章的添加操作
		
		int addCard = smallticketService.addCard(map);
		
		return addCard;		
	}
	/**
	 * 查询所有抵扣卡
	 * @author xuwei
	 * time 2019/02/19 15:58
	 * */
	@RequestMapping("/findAllCard.do")
	public String findAllCard(HttpServletRequest request) {
		

		List<Map<String, Object>> findAllCard = smallticketService.findAllCard();
		for (Map<String, Object> map : findAllCard) {
			map.put("c_btime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_btime").toString()))));
			map.put("c_etime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_etime").toString()))));
			map.put("c_creattime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_creattime").toString()))));
		}
		request.setAttribute("findAllCard",findAllCard);
		return "jsp/zhekou/zhekou_manage";		
	}
	/**
	 * 修改抵扣卡状态
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@ResponseBody
	@RequestMapping("/uodateCardStatus.do")
	public int uodateCardStatus(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		int status = smallticketService.updateCardStatus(map);

		return status;		
	}
	/**
	 * 查看抵扣卡详情
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@RequestMapping("/findCardDetails.do")
	public String findCardDetails(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		System.out.println("amp :  "+map);
		Map<String, Object> findCardDetails = smallticketService.findCardDetails(map);
		findCardDetails.put("c_btime", XVDateUtils.MillisFormat(Integer.valueOf((findCardDetails.get("c_btime").toString()))));
		findCardDetails.put("c_etime", XVDateUtils.MillisFormat(Integer.valueOf((findCardDetails.get("c_etime").toString()))));
		findCardDetails.put("c_creattime", XVDateUtils.MillisFormat(Integer.valueOf((findCardDetails.get("c_creattime").toString()))));
		
		request.setAttribute("cardDetails",findCardDetails);
		
		List<Map<String, Object>> allPromotionByFlagAndStatus = promotionService.findAllPromotionByFlagAndStatus();
		request.setAttribute("allPromotionByFlagAndStatus", allPromotionByFlagAndStatus);
		return "jsp/zhekou/zhekou_update";		
	}
	
	/**
	 * 修改抵扣卡信息
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@ResponseBody
	@RequestMapping("/updateCardDetails.do")
	public int updateCardDetails(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		
		System.out.println("参数："+map);
		map.put("c_btime", XVDateUtils.stringDateToMillis(map.get("c_btime").toString()));
		map.put("c_etime", XVDateUtils.stringDateToMillis(map.get("c_etime").toString()));
		
		//System.out.println(CommonUtils.StripHT(map.get("t_info").toString()));
		//下面进行文章的添加操作
		int result = smallticketService.updateCardDetails(map);
		return result;		
	}

}
